package com.special.threademo.httpreflect;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author LiuYC
 * @version 1.0.0
 * @create 2020-12-24 9:02
 */
public class HTttpClientTest {
    public static void main(String[] args) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://www.jb51.net");
        RequestConfig build = RequestConfig.custom().setConnectTimeout(1000).setSocketTimeout(1000).setConnectionRequestTimeout(1000).build();
        httpGet.setConfig(build);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
        } catch (ClientProtocolException e) { // 协议转换异常
            e.printStackTrace();
        } catch (IOException e) { // io异常
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();
        try {
            EntityUtils.toString(entity, "utf-8");
        } catch (ParseException e) { // 解析异常
            e.printStackTrace();
        } catch (IOException e) { // io异常
            e.printStackTrace();
        }
        try {
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
